package im.vinci.server.music.persistence;

import im.vinci.server.music.domain.MusicStyle;
import im.vinci.server.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

/**
 * Created by mingjie on 17/3/13.
 */
@Repository
public class MusicStyleFileMapper {

    private static final String DATA_FILE = "src/main/resources/MusicStyle.json";

    private Logger logger = LoggerFactory.getLogger(getClass());

    private List<MusicStyle> data;

    @PostConstruct
    public void init() {
        try {
            logger.info("Read MusicStyle");

            data = readFile(DATA_FILE);

            logger.info("Read MusicStyle data, size: {}", data.size());
        } catch (Exception e) {
            logger.error("Read MusicStyle error", e);
        }
    }

    private List<MusicStyle> readFile(String fileName) throws Exception {
        return JsonUtils.getHttpObjectMapperInstance().readValue(new File(fileName), List.class);
    }

    public List<MusicStyle> getData() {
        return data;
    }
}
